#include<iostream>     /* cout and endl */
#include<cstdlib>     /* srand, rand */
#include<ctime>     /* time */
#include<iomanip>     /* setw */

#include "histogram.h"

using std::cout;
using std::endl;
using std::setw;
using std::srand;
using std::rand;

histogram::histogram( int size )
{
    _size = ( size > 0 ? size : 10 );

    // Sorteia entre 0 e 99
    srand ( time( 0 ) );
    for( int i = 0; i < _size; i++ )
        _elements.push_back( rand() % 100 );
}


void histogram::printDistribution()
{
    std::vector<int> histogram;
    int i;

    // Limpa o histograma
    for( i = 0; i < 10; i++ )
        histogram.push_back(0);

    // Calcula histograma
    for( i = 0; i < _size; i++ )
        histogram[ _elements[i] / 10 ]++;

    // Mostra os '*'
    for( i = 0; i < 10; i++ )
    {
        cout << "[" << setw(2) << i*10 << " - " << setw(2) << i*10 + 9 << "] ";
        for( int j = 0; j < histogram[i]; j++ )
        {
            cout << "*";
        }
        cout << " (" << histogram[i] << ")" << endl;
    }
}


